import { CHANGE_DONE_TODO, DEL_TODO } from '../action/actionTypes'

// state 初始值
const initState = {
  list: [
    { id: 1, content: '吃饭', done: false },
    { id: 2, content: '睡觉', done: true },
    { id: 3, content: '打代码', done: false },
  ],
}

// reducer 函数 (state, action)=> newState
export default function todoReducer(state = initState, action) {
  // console.log(action)
  switch (action.type) {
    case 'todo/add':
      return { ...state, list: [action.payload, ...state.list] }
    // 👍 通过常量存储字符串，书写时可以有代码提示
    case DEL_TODO:
      return { ...state, list: state.list.filter((v) => v.id !== action.id) }
    case CHANGE_DONE_TODO:
      return {
        ...state,
        list: state.list.map((item) => {
          if (item.id === action.id) {
            return { ...item, done: !item.done }
          } else {
            return item
          }
        }),
      }
    default:
      return state
  }
}
